#include "CSetAnimationFileCommand.h"
#include "CUserInterface.h"
#include "CLevelEditor.h"

extern CGlobals* g;
extern CUserInterface* ui;
extern CLevelEditor* editor;

CSetAnimationFileCommand::CSetAnimationFileCommand (int id, stringw newAnimationFile)
{
	m_id = id;
	CSceneObject* obj = editor->getSceneObjectFromID(id);
	m_oldAnim = obj->m_UserData->getAttributeAsInt("@CurrentAnim");
	m_oldAnimationFile = obj->m_UserData->getAttributeAsStringW("AnimationFile");
	m_newAnimationFile = newAnimationFile;
	setDescription(L"change AnimationFile");
}

void CSetAnimationFileCommand::redo ()
{
	CSceneObject* obj = editor->getSceneObjectFromID(m_id);
	obj->m_UserData->setAttribute("AnimationFile", m_newAnimationFile.c_str());
	obj->loadAnimations();
	obj->m_UserData->setAttribute("@CurrentAnim", 0);
	if (obj->m_Animations.size())
		((IAnimatedMeshSceneNode*)obj->m_SceneNode)->setFrameLoop(
			obj->m_Animations[0].startFrame, obj->m_Animations[0].endFrame);
	editor->setSelectedNode(obj->m_SceneNode);
	ui->createSelectedObjectProperties();
}

void CSetAnimationFileCommand::undo ()
{
	CSceneObject* obj = editor->getSceneObjectFromID(m_id);
	obj->m_UserData->setAttribute("AnimationFile", m_oldAnimationFile.c_str());
	obj->loadAnimations();
	obj->m_UserData->setAttribute("@CurrentAnim", m_oldAnim);
	if (obj->m_Animations.size())
		((IAnimatedMeshSceneNode*)obj->m_SceneNode)->setFrameLoop(
			obj->m_Animations[m_oldAnim].startFrame, obj->m_Animations[m_oldAnim].endFrame);
	editor->setSelectedNode(obj->m_SceneNode);
	ui->createSelectedObjectProperties();
}
